<template>
	<view>
		<uni-search-bar @confirm="search" :focus="true" v-model="searchValue" @blur="blur" @focus="focus" @input="input"
			@cancel="cancel" @clear="clear">
		</uni-search-bar>
	</view>
	<view>
		<swiper circular :indicator-dots="true" :autoplay="true" :interval="3000" :duration="500">
			<swiper-item v-for="item in swiperList">
				<image style="width: 750rpx;height: 400rpx;" :src="item.img"></image>
			</swiper-item>

		</swiper>
	</view>
	<view class="box">
		<ul class="box-ul">
			<li class="box-li" v-for="item in arrList" @click='godetailsFn(item.proid)'>
				<image style="width: 340rpx;height: 340rpx;" :mode="scaleToFill" :src="item.img1"></image>
				<view class="text">{{item.proname}}</view>
				<view class="con">
					<view class="price">
						￥{{item.originprice}}
					</view>
					<view class="add">
						加入购物车
					</view>
				</view>
			</li>
		</ul>
	</view>
</template>

<script setup>
	import {
		onPullDownRefresh,
		onReachBottom
	} from "@dcloudio/uni-app"
	import {
		ref
	} from 'vue';


	const arrList = ref([])
	const swiperList = ref([])
	let num = ref(1)

	function getDataList() {
		uni.request({
			url: 'http://47.94.6.186:3001/api/pro/list',
			method: 'GET',
			data: {
				count: num.value,
				limitNum: 10
			},
			header: {
				// 'content-type': 'application/json',
				// "token": "xxxxxxxx"
			},
			success(res) {
				console.log(res.data);
				console.log(res.data.data);
				console.log(num.value)
				arrList.value = [...arrList.value].concat(res.data.data)
				console.log(arrList.value)
			},
			fail() {},
			complete() {}
		});
	}

	function swiperFn() {
		uni.request({
			url: 'http://47.94.6.186:3001/api/banner/list',
			method: 'GET',
			header: {
				// 'content-type': 'application/json',
				// "token": "xxxxxxxx"
			},
			success(res) {
				console.log(res.data);
				console.log(res.data.data);
				swiperList.value = res.data.data
			},
			fail() {},
			complete() {}
		});
	}

	function godetailsFn(id){
		console.log('go details')
		console.log(id)
		uni.navigateTo({
					url: "/pages/details/details?id="+id
				})
	}
	
	onPullDownRefresh(() => {
		console.log('refresh');
		arrList.value = []
		num.value = 1
		getDataList()
		uni.stopPullDownRefresh();
	})

	onReachBottom(() => {
		console.log('ReachBottom');
		setTimeout(function() {
			num.value = num.value + 1
			console.log(num.value)
			getDataList()
		}, 500);
	})

	getDataList()
	swiperFn()
</script>

<style>
	* {
		padding: 0rpx;
		margin: 0rpx;
		list-style: none;
	}

	.box-ul {
		display: flex;
		justify-content: space-evenly;
		flex-wrap: wrap;
	}

	.box-li {
		width: 340rpx;
		height: 470rpx;
		background-color: antiquewhite;
		margin-bottom: 20rpx;
		overflow: hidden;
	}

	.text {
		display: inline-block;
		font-size: 20rpx;
		margin-left: 20rpx;
		width: 300rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.con {
		display: flex;
		padding: 5rpx;
		justify-content: space-between;
	}

	.price {
		width: 160rpx;
		height: 60rpx;
		background-color: aqua;
		font-size: 20rpx;
		line-height: 60rpx;
	}

	.add {
		width: 160rpx;
		height: 60rpx;
		background-color: aqua;
		font-size: 30rpx;
		line-height: 60rpx;
	}
</style>